package com.lab.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lab.dao.CollegeDao;
import com.lab.dao.CourseDao;
import com.lab.dao.MajorDao;
import com.lab.dao.ScoreDao;
import com.lab.dao.StudentDao;
import com.lab.dao.UserDao;
import com.lab.dao.impl.CollegeDaoImpl;
import com.lab.dao.impl.CourseDaoImpl;
import com.lab.dao.impl.MajorDaoImpl;
import com.lab.dao.impl.ScoreDaoImpl;
import com.lab.dao.impl.StudentDaoImpl;
import com.lab.dao.impl.UserDaoImpl;
import com.lab.po.College;
import com.lab.po.Major;
import com.lab.po.Score;
import com.lab.po.Student;
import com.lab.po.User;


//逻辑处理及页面跳转
@WebServlet("/preServlet")
public class PreServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = (String) request.getParameter("method");
		if ("studentAdd".equals(method)) {
			studentAdd(request, response);
		}else if ("updateStudent".equals(method)) {
			updateStudent(request, response);
		}else if("addColMaj".equals(method)){
			addColMaj(request, response);
		}else if("updateCollege".equals(method)){
			updateCollege(request, response);
		}else if("updateMajor".equals(method)){
			updateMajor(request,response);
		}else if("preupdatetepwd".equals(method)){
			preupdatetepwd(request, response);
		}else if("addCourse".equals(method)){
			addCourse(request, response);
		}else if("updateCourse".equals(method)){
			updateCourse(request,response);
		}else if("updateScore".equals(method)){
			updateScore(request,response);
		}else if("updateStuPwd".equals(method)){
			updateStuPwd(request,response);
		}else if("showScore".equals(method)){
			showScore(request,response);
		}else if("stupwd".equals(method)){
			stupwd(request,response);
		}else if("alterstupwd".equals(method)){
			alterstupwd(request,response);
		}else if(method==null){
			updatetepwd(request, response);
		}
	}
	
	
	//学生修改密码之后实现跳转
	private void alterstupwd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		
		 int stId=Integer.parseInt(request.getSession().getAttribute("stId").toString());
		 String pwd=request.getParameter("repassword");
		 Map map=new HashMap();
		 map.put("stId", stId);
		 map.put("pwd", pwd);
		 UserDao userdao=new UserDaoImpl();
		 int a=userdao.updateStuPwd(map);
		 if(a!=0){
			 String mainPage = "updatepwdSuccess.jsp";
			 request.setAttribute("mainPage", mainPage);
		 }else{
			 request.setAttribute("error", "更新失败");
		 }
		    request.getRequestDispatcher("stumain.jsp").forward(request, response);
		
	}

	//学生修改密码之前实现跳转
	private void stupwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String mainPage = "stupwd.jsp";
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("stumain.jsp").forward(request, response);
		
	}

	//查询成绩实现页面跳转
	private void showScore(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int stId=Integer.parseInt(request.getSession().getAttribute("stId").toString());
		ScoreDao sd =new ScoreDaoImpl();
		List<Score> scoreList=sd.queryStuScore(stId);
		request.setAttribute("count", scoreList.size());
		request.setAttribute("stId", stId);
		request.setAttribute("scoreList", scoreList);
		String mainPage="showScore.jsp";
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("stumain.jsp").forward(request, response);
		
	}

	//重置密码之前实现页面跳转
	private void updateStuPwd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String mainPage="updateStuPwd.jsp";
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("main.jsp").forward(request, response);
		
	}

	//录入成绩之前实现页面跳转
	private void updateScore(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int stId = Integer.parseInt(request.getParameter("id"));
		ScoreDao sd =new ScoreDaoImpl();
		List<Score> scoreList=sd.queryStuScore(stId);
		request.setAttribute("count", scoreList.size());
		request.setAttribute("stId", stId);
		request.setAttribute("scoreList", scoreList);
		String mainPage="updateScore.jsp";
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("main.jsp").forward(request, response);
	}

	//更新课程之前实现页面跳转
	private void updateCourse(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int cou_id=Integer.parseInt(request.getParameter("cou_id"));
		CourseDao cd=new CourseDaoImpl();
		String cou_name=cd.queryCouName(cou_id);
		request.setAttribute("cou_id", cou_id);
		request.setAttribute("cou_name", cou_name);
		String mainPage="updateCourse.jsp";
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("main.jsp").forward(request, response);
	}

	//新增课程之前实现页面跳转
	private void addCourse(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String mainPage="addCourse.jsp";
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("main.jsp").forward(request, response);
		
	}

	//更新专业信息之前实现页面跳转
	private void updateMajor(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int maj_id=Integer.parseInt(request.getParameter("maj_id"));
		MajorDao majordao = new MajorDaoImpl();
		Major major=majordao.queryMajorByMaj_id(maj_id);
		CollegeDao collegedao=new CollegeDaoImpl();
		List<College> collegeList=collegedao.queryCollege();
		request.setAttribute("collegeList", collegeList);
		request.setAttribute("major", major);
		String mainPage = "updateMajor.jsp";
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("main.jsp").forward(request, response);
	}

	//更新学院的信息前-实现页面跳转
	private void updateCollege(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int col_id = Integer.parseInt(request.getParameter("col_id"));
		System.out.println(col_id);
		CollegeDao collegedao = new CollegeDaoImpl();
		String col_name = collegedao.queryCollegeById(col_id);
		request.setAttribute("col_id", col_id);
		request.setAttribute("col_name", col_name);
		String mainPage="updateCollege.jsp";
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("main.jsp").forward(request, response);
	}

	//新增学院或专业之前实现页面跳转
	private void addColMaj(HttpServletRequest request,
			HttpServletResponse response)  throws ServletException, IOException {
		CollegeDao collegedao=new CollegeDaoImpl();
		List<College> collegeList=collegedao.queryCollege();
		request.setAttribute("collegeList", collegeList);
		String mainPage = "collegeAdd.jsp";
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("main.jsp").forward(request, response);
		
	}

	//添加学生的信息前-实现页面跳转
	public void studentAdd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		CollegeDao collegedao=new CollegeDaoImpl();
		List<College> collegeList=collegedao.queryCollege();
		MajorDao majordao=new MajorDaoImpl();
		Map map=new HashMap();
		map.put("col_id", collegeList.get(0).getCol_id());
		List<Major> majorList=majordao.queryMajor(map);
		request.setAttribute("collegeList", collegeList);
		request.setAttribute("majorList", majorList);
		String mainPage = "studentAdd.jsp";
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("main.jsp").forward(request, response);
	}
	//更新学生的信息前-先查询出学生的信息，并实现页面跳转
	public void updateStudent(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		StudentDao studao = new StudentDaoImpl();

		Student student = studao.findStudentByid(id);
		request.setAttribute("student", student);
		CollegeDao collegedao=new CollegeDaoImpl();
		List<College> collegeList=collegedao.queryCollege();
		MajorDao majordao=new MajorDaoImpl();
		Map map=new HashMap();
		map.put("col_id", student.getCol_id());
		List<Major> majorList=majordao.queryMajor(map);
		request.setAttribute("collegeList", collegeList);
		request.setAttribute("majorList", majorList);
		String mainPage = "studentUpdate.jsp";
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("main.jsp").forward(request, response);
	}


	//更改管理员密码-实现页面跳转
	public void preupdatetepwd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String mainPage = "updatepwd.jsp";
		request.setAttribute("mainPage", mainPage);
		request.getRequestDispatcher("main.jsp").forward(request, response);
	}
	//更改管理员密码
	public void updatetepwd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		 HttpSession session=request.getSession();
		 String username=(String)session.getAttribute("username");
		 String repassword=request.getParameter("repassword");
		 User user=new User(username,repassword);
		 UserDao userdao=new UserDaoImpl();
		 int a=userdao.updateUserPassWord(user);
		 if(a!=0){
			 String mainPage = "updatepwdSuccess.jsp";
			 request.setAttribute("mainPage", mainPage);
		 }else{
			 request.setAttribute("error", "更新失败");
		 }
		    request.getRequestDispatcher("main.jsp").forward(request, response);
	}
	
	
}
